package com.ch.home.service.serviceImpl.userServiceImpl;

import com.ch.home.bean.User;
import com.ch.home.mapper.userMessageMapper.UserMapper;
import com.ch.home.service.serviceInterface.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;

/**
 * Created by CHEN on 2019/8/15.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public String getUserName(Long id) {

        System.out.println("id=" + id);
        return userMapper.selectNameById(id);
    }

    /**
     * 根据username、userId、QQ、phone是否存在
     *
     * @param user
     * @return
     */
    @Override
    public boolean judgeExists(User user) {

        Integer integer = userMapper.judgeExists(user);
        //存在
        if (integer == 1) {
            return true;
        } else {
            //不存在
            return false;
        }
    }

    @Override
    public User getUserByCondition(Long id, String username, String password, String phone, String QQ, String email) {
        User user = new User(id, username, password, phone, QQ, email);
        System.out.println(user);
        return userMapper.selectByCondition(user);
    }

    @Override
    public HashMap getUserByCondition(User user) {
        HashMap<Object, Object> result = new HashMap<>( );
        System.out.println(user);
        User user1 = userMapper.selectByCondition(user);
        if (user1 == null) {
            result.put("message", "fail");
        } else {
            result.put("message", "success");
        }
        result.put("value", user1);
        return result;
    }

    /**
     * 登录，账号密码登录，手机QQ登录，邮箱密码登录
     *
     * @param user
     * @return
     */
    @Override
    public HashMap login(User user) {
        HashMap<String, Object> result = new HashMap( );
        User user1 = new User( );
        if (user.getUsername( ) != null && user.getPassword( ) != null) {
            //账号密码登录
            user1.setUsername(user.getUsername( ));
            user1.setPassword(user.getPassword( ));
            System.out.println(user1);
            User user2 = userMapper.selectByCondition(user1);
            if (user2==null) {
                result.put("message", "Username Or Password Error!");
            } else {
                result.put("message", "login success");
            }
            result.put("value", user2);
        } else if (user.getPhone( ) != null) {
            //手机号登录，默认已验证
            user1.setPhone(user.getPhone( ));

            User user2 = userMapper.selectByCondition(user1);
            if (user2==null) {
                result.put("message", "Please Register!");
            } else {
                result.put("message", "login success");
            }
            result.put("value", user2);

        } else if (user.getQQ( ) != null) {
            //QQ第三方登录，默认已验证
            user1.setQQ(user.getQQ( ));
            User user2 = userMapper.selectByCondition(user1);
            if (user2==null) {
                result.put("message", "Please Register!");
            } else {
                result.put("message", "login success");
            }
            result.put("value", user2);

        } else if (user.getEmail( ) != null && user.getPassword( ) != null) {
            //邮箱密码登录
            user1.setEmail(user.getEmail( ));
            user1.setPassword(user.getPassword( ));
            User user2 = userMapper.selectByCondition(user1);
            if (user2==null) {
                result.put("message", "Username Or Password Error!");
            } else {
                result.put("message", "login success");
            }
            result.put("value", user2);
        }
        return result;
    }

    /**
     * 账号密码注册，邮箱注册
     *
     * @param user
     * @param exists
     * @return
     */
    @Override
    public HashMap register(User user, boolean exists) {
        HashMap<Object, Object> result = new HashMap( );

        String portrait = "http://www.aliyouth.cn/repository/Image/CodeResource.png";
        //一些默认值
        user.setPortrait(portrait);
        user.setAuthority(0);
        user.setStatus(1);
        user.setIntegral((long) 0);
        user.setCollectNum(0);

        //重复
        if (exists == true) {
            result.put("message", "repetition");
        } else {
            if (userMapper.register(user) == 1) {
                System.out.println(user);
                result.put("message", "seccess");
            } else {
                System.out.println(user);
                result.put("message", "fail");
            }
        }
        return result;
    }

    /**
     * 手机QQ，注册
     *
     * @param user
     * @param exists
     * @return
     */
    @Override
    public HashMap otherRegister(User user, boolean exists) {
        HashMap<Object, Object> result = new HashMap( );

        if (user.getUsername( ) == null && user.getPassword( ) == null) {
            user.setPassword("123456");
            if (user.getQQ( ) != null) {
                user.setUsername(user.getQQ( ));
            } else if (user.getPhone( ) != null) {
                user.setUsername(user.getPhone( ));
            } else {
                result.put("message", "Information loss");
                return result;
            }
            System.out.println(user);
        }
        if (exists == true) {
            //重复
            result = login(user);
        } else {
            //不重复,注册
            result = register(user, exists);
        }
        return result;
    }

//    /**
//     * 用于获取map中包含的数据,map为传入的Json封装而成
//     *
//     * @param map
//     * @return
//     */
//    public User getUserFromMap(Map<Object, Object> map) {
//        //假如关键词中没有，map.get()返回null
//        Long userId;
//        String username = (String) map.get("username");
//        String password = (String) map.get("password");
//        Integer age = (Integer) map.get("age");
//        Integer gender = (Integer) map.get("gender");
//        String phone = (String) map.get("phone");
//        String QQ = (String) map.get("QQ");
//        String email = (String) map.get("email");
//        String portrait = (String) map.get("portrait");
//        Integer authority = (Integer) map.get("authority");
//        Integer status = (Integer) map.get("status");
//        Long integral;
//        Integer fansNum = (Integer) map.get("fansNum");
//        Integer followNum = (Integer) map.get("followNum");
//
//        if (map.get("userId") == null) {
//            userId = null;
//        } else {
//            userId = (Long) map.get("userId");
//        }
//
//        if (map.get("integral") == null) {
//            integral = null;
//        } else {
//            integral = Long.valueOf((Integer) map.get("integral"));
//        }
//
//        User user = new User(userId, username, password, age, gender, phone, QQ, email, portrait, authority, status, integral, fansNum, followNum);
//        return user;
//    }


    /**
     * gender,phone,email,QQ,portrait,status,integral等属性修改
     * 条件是：userId，username
     * 其中integral只传改变的数字（负数也可，移动端判断够不够减的问题，暂时默认永远够减）
     *
     * @param user
     */
    @Override
    public HashMap update(User user) {
        HashMap result = new HashMap<String, String>( );
        Integer re = userMapper.updateUser(user);
        if (re == 1) {
            System.out.println(user);
            result.put("message", "seccess");
        } else {
            System.out.println(user);
            result.put("message", "fail");
        }
        return result;
    }


    /**
     * 返回用户信息列表
     *
     * @return
     */
    @Override
    public LinkedList<User> listUser() {
        return userMapper.listUser( );
    }

}
